package net.dgg.dggcloud.service.jrcrm.service.orf;

import net.dgg.dggcloud.core.json.JsonUtil;
import net.dgg.dggcloud.core.util.NJCodeUtil;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.jrcrm.dao.orf.OrderContactDao;
import net.dgg.dggcloud.service.jrcrm.entity.orf.OrderContact;
import net.dgg.dggcloud.service.entity.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 鲜林材
 * 订单联系
 * @create 2017-11-06 15:01
 **/
@Service
public class OrderContactService extends BaseService {

    @Autowired
    private OrderContactDao orderContactDao;//订单联系

    @Resource
    private NJCodeUtil nJCodeUtil;

    /**
     * 保存订单联系信息
     *
     * @param orderContact
     * @param user
     */
    @Transactional
    public void saveOrderContact(OrderContact orderContact, User user) {
        //设置用户信息
        try {
            orderContact.setCreateUser(user);
            long id = this.keyWorker.nextId() ;
            orderContact.setId(id);
            this.repository.save(orderContact);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("保存订单联系异常："+e.getMessage(),10006);
        }
    }

    /**
     * 查询订单联系信息通过map
     *
     * @param map 查询条件<br/>
     *            ordeId 订单id<br/>
     *            name  借贷人名称<br/>
     * @return
     */
    public List<Map> queryOrderContactListByMap(Map map) {

        return orderContactDao.queryOrderContactListByMap(map);
    }

    /**
     * 通过实体对应的属性，查询信息
     *
     * @param properName:实体属性
     * @param value:实体属性对应值
     * @return
     */
    public List<OrderContact> queryOrder(String properName, Object value) {
        return this.repository.findByProperty(OrderContact.class, properName, value);
    }

    /**
     * 保存订单联系人信息
     * @param map :联系人集合<br/>
     *     user 用户 <br/>
     *     orderId 订单id <br/>
     *     loaninfos  借款人信息 <br/>
     */
    @Transactional
    public void saveOrUpdateBatchOrderContact(Map map) {
        try{
            String loaninfos = map.get("loaninfos")==null ?"" :map.get("loaninfos").toString();
            if(StringUtils.isNotEmpty(loaninfos)) {
                List<OrderContact> orderContacts = JsonUtil.json2List(loaninfos.toString(), OrderContact.class);
                Long orderId = (Long) map.get("orderId");
                User user = (User) map.get("user");
                if (null != orderContacts && !orderContacts.isEmpty()) {
                    for (OrderContact orderContact : orderContacts) {
                        orderContact.setOrderId(orderId);
                        if (orderContact.getId() != null) {
                            OrderContact oldOrderContact = this.getByIdHiber(orderContact.getId());
                         if(oldOrderContact!=null){
                              oldOrderContact.setName(orderContact.getName());
                              oldOrderContact.setCardId(orderContact.getCardId());
                              oldOrderContact.setNoBak(orderContact.getNo());
                              oldOrderContact.setNo(nJCodeUtil.encrypt(orderContact.getNo()));
                              oldOrderContact.setRelationshipOfCus(orderContact.getRelationshipOfCus());
                              oldOrderContact.setChildrenNum(orderContact.getChildrenNum());
                              oldOrderContact.setMaritalStatus(orderContact.getMaritalStatus());
                              oldOrderContact.setNoKnow(orderContact.getNoKnow());
                              oldOrderContact.setIsSelf(orderContact.getIsSelf());
                              oldOrderContact.setLenderStatus(orderContact.getLenderStatus());
                            }
                            this.updateOrderContact(oldOrderContact,user);
                        } else {
                            orderContact.setNoBak(orderContact.getNo());
                            orderContact.setNo(nJCodeUtil.encrypt(orderContact.getNo()));
                            this.saveOrderContact(orderContact, user);
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw  new BizException("保存订单联系人异常:"+e.getMessage(),10006);
        }
    }


    /**
     *更新订单联系信息
     *
     * @param orderContact
     * @param user
     */
    @Transactional
    public void updateOrderContact(OrderContact orderContact, User user) {
        //设置用户信息
        try {
            orderContact.setUpdaterUser(user);
            this.repository.saveOrUpdate(orderContact);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("更新订单联系异常:"+e.getMessage(),10006);
        }
    }

    /**
     *  根据ID，获取订单联系人
     * @param id
     * @return
     */
    public OrderContact getByIdHiber(Long id){
        return this.repository.get(OrderContact.class,id);
    }


    /**
     *更新订单联系信息
     *
     * @param orderContact
     * @param user
     */
    @Transactional
    public void updateOrderContactBy(OrderContact orderContact, User user) {
        //设置用户信息
        try {
            orderContact.setUpdaterUser(user);
            this.repository.saveOrUpdate(orderContact);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("更新订单联系异常:"+e.getMessage(),10006);
        }
    }

    /**
     * 通过订单id删除订单业务信息
     * @param parameterMap
     * 传入orderId
     */
    @Transactional
    public void deleteOrderContactByMap(Map parameterMap) {
        orderContactDao.deleteOrderContactByMap(parameterMap);
    }
}
